package com.kingsoft.dc.khaos.module.spark.request.encode;

import org.apache.log4j.Logger;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * create by yansu on 2019/07/02
 */
public class Md5Util extends EncoderUtil {
    private static final Logger logger = Logger
            .getLogger(Md5Util.class);
    private Md5Util _instance;

    public Md5Util getInstance() {
        if (_instance == null) {
            _instance = new Md5Util();
        }
        return _instance;
    }

    public String encode(String str) {
        logger.info("encode by md5");
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        BASE64Encoder base64en = new BASE64Encoder();
        assert md5 != null;
        String newstr = base64en.encode(md5.digest(str.getBytes(StandardCharsets.UTF_8)));
        return newstr;
    }
}
